﻿using System;
using System.Collections.Generic;
using System.Numerics;
using System.Reflection;
using System.Text;
using WaveSim.Core.Data.Parser;
using WaveSim.Core.Equations.Galerkin;

namespace MicroWave
{
    public class Rectangle : BaseDielectric, ICodable
    {
        private static Dictionary<string, PropertyInfo> properties;

        static Rectangle()
        {
            properties = new Dictionary<string, PropertyInfo>();
            foreach (var property in typeof(Rectangle).GetProperties())
            {
                properties.Add(property.Name, property);
            }
        }

        public double X0 { get; set; }

        public double X1 { get; set; }

        public double Z0 { get; set; }

        public double Z1 { get; set; }

        public double ReEps { get; set; }

        public double ImEps { get; set; }

        /// <summary>
        /// Returns a value of epsilon
        /// </summary>
        /// <param name="coordinate">x and z values</param>
        /// <returns></returns>
        public override Complex Eps(params double[] coordinate)
        {
            double x = coordinate[0];
            double z = coordinate[1];
            if (x >= X0 && x <= X1 &&
                z >= Z0 && z <= Z1)
            {
                return new Complex(ReEps, ImEps);
            }
            return DefaultValue;
        }

        /// <summary>
        /// Returns a value of mu
        /// </summary>
        /// <param name="coordinate">x and z values</param>
        /// <returns></returns>
        public override Complex Mu(params double[] coordinate)
        {
            return DefaultValue;
        }

        #region ICodable
        string ICodable.Code()
        {
            StringBuilder code = new StringBuilder();
            code.AppendFormat("#{0}", this.GetType().Name);
            foreach (var keyValue in properties)
            {
                code.AppendFormat("\n\t{0}={1}", keyValue.Key, keyValue.Value.GetValue(this, null));
            }
            return code.ToString();
        }

        object ICodable.Decode(string txt)
        {
            string[] lines = txt.Split('\n');
            foreach (var code in lines)
            {
                string[] keyValue = code.Split('=');
                if (keyValue.Length <= 1)
                {
                    continue;
                }

                string key = Process(keyValue[0]);
                string val = Process(keyValue[1]);
                if (properties.ContainsKey(key))
                {
                    Type t = properties[key].PropertyType;
                    properties[key].SetValue(this, Convert.ChangeType(val, t), null);
                }
            }
            return this;
        }
        #endregion

        private static string Process(string str)
        {
            return str
                .Replace("\r", String.Empty)
                .Replace("\t", String.Empty)
                .Replace("\n", String.Empty)
                .Trim();
        }
    }
}
